﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("付款单保存后源单明细付款申请单号、采购订单号拼接到付款单备注上")]
    public class BackNumber : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            foreach (DynamicObject item in e.DataEntitys)
            {
                string FID = "", fbillno = "", FPURORDERNO = "", Note = "", join="",join1 = "", join2 = "", FENTRYID="", F_WNEG_TEXT2_SRC="", FDESCRIPTION="";
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FID = Convert.ToString(item["ID"]);
                }
                string SQL = "/*dialect*/" + string.Format(@"select t1.FENTRYID,t1.Fksqno fbillno,t1.F_WNEG_TEXT2_SRC_E F_WNEG_TEXT2_SRC,t1.F_Note FDESCRIPTION from T_AP_PAYBILL t
                left join T_AP_PAYBILLENTRY t1 on t.fid=t1.FID
                where t.fid={0} and  t.FBILLTYPEID='D9652BC4-1420-4D3B-A214-2509BC9BF925'", FID);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                if(DynamData.Count>0)
                {
                    for(int i=0;i<DynamData.Count;i++)
                    {
                        FENTRYID = Convert.ToString(DynamData[i]["FENTRYID"]);
                        fbillno = Convert.ToString(DynamData[i]["fbillno"]);
                        F_WNEG_TEXT2_SRC = Convert.ToString(DynamData[i]["F_WNEG_TEXT2_SRC"]);
                        if (!fbillno.Equals("")&& join1.Equals(""))
                        {
                            join1 = fbillno;
                        }
                        if(!fbillno.Equals("") && !join1.Equals(""))
                        {
                            join1 = fbillno + "," + join1;
                        }
                        if (!F_WNEG_TEXT2_SRC.Equals("") && join.Equals(""))
                        {
                            join = F_WNEG_TEXT2_SRC;
                        }
                        if (!F_WNEG_TEXT2_SRC.Equals("") && !join.Equals(""))
                        {
                            join = F_WNEG_TEXT2_SRC + "," + join;
                        }
                        if (!FDESCRIPTION.Equals("") && join2.Equals(""))
                        {
                            join2 = FDESCRIPTION;
                        }
                        if (!FDESCRIPTION.Equals("") && !join2.Equals(""))
                        {
                            join2 = FDESCRIPTION + "," + join2;
                        }
                    }
                    Note = join+ ","+join1 + "," + join2;

                    string output = string.Join(",",Note.Split(',').Distinct().ToArray());
                    string SQL0 = "/*dialect*/" + string.Format(@"update T_AP_PAYBILLENTRY SET  FCOMMENT='{1}' where FID='{0}'", FID, output);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                }
            }
        }
    }
}
